const connection = require('../../data/index.js')


module.exports.going = (req, res) => {
    if(!req.session.login){
      
        res.redirect('/login');
        return;
      }
    res.render('going')
}
module.exports.show = (req, res) => {
    res.render('show')
}
module.exports.dynamic = (req, res) => {
    res.render('dynamic')
}

module.exports.map = (req, res) => {
    if(!req.session.login){
        let results = [0];
        res.render('map',{re:results}) ;
      }else{
          let results = [1];
          res.render('map',{re:results});
        
        }
    
}

module.exports.writeAdd = (req, res) => {
    // 时间
    let date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day1 = date.getDate() + 1;
    var hour = date.getHours(); //时
    var minute = date.getMinutes(); //分
    var second = date.getSeconds(); //秒

    if (month < 10) {
        month = "0" + month;
    }
    if (day1 < 10) {
        day1 = "0" + day;
    }
    var day = year + "-" + month + "-" + day1 + " " + hour + ":" + minute + ":" + second;
    console.log(day)
    // .............................................................................................
    console.log(req.query.content);
    connection.query('insert into study (study_content,study_time) values(?,?)', [req.query.content,day], (err, result) => {
        if (err) {
            return console.log(err);
        }
        if (result) {
            res.json({
                code: '1101',
                message: '保存成功'
            })
        }
    })
}

module.exports.showUser = (req, res) => {
    connection.query('select * from study', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}


module.exports.delUser = (req, res) => {
    connection.query('delete from study where study_id= ' + req.query.id, (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json({
                code: '1201',
                message: '删除成功'
            })
        }
    })
}

// 更新
module.exports.userUpdate = (req, res) => {
    // console.log(params);
    connection.query('update study set study_content=? where study_id=?', [req.body.update, req.body.id], (error, result) => {
        if (error) return console.log(error);

        //   console.log(results);
        if (result) {
            res.json({
                code: '1002',
                message: '更新成功'
            })
        }
    })
}

module.exports.showUser1 = (req, res) => {
    connection.query('select * from study', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

// 回显
module.exports.userFind = (req, res) => {
    console.log(req.query);
    connection.query('select study_content from study where study_id =?', [req.query.id], (error, results) => {
        if (error) return console.log(error);

        // console.log(results);
        res.json(results);
    })
}